﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Messaging;

namespace CometTesterMessagePump
{
    public partial class MainForm : Form
    {

        private string _message = null;

        public MainForm()
        {
            InitializeComponent();
        }

        private void sendMessagesButton_Click(object sender, EventArgs e)
        {
            int bytesPerMessage = Convert.ToInt32(_bytesPerMessageTextBox.Text);
            int millisecondsPerMessage = Convert.ToInt32(_msBetweenMessagesTextBox2.Text);

            StringBuilder messageBuilder = new StringBuilder();
            for (int i = 0; i < bytesPerMessage; i++)
            {
                messageBuilder.Append("A");
            }

            _message = messageBuilder.ToString();
            _messagePumpTimer.Interval = millisecondsPerMessage;
            _messagePumpTimer.Start();

        }

        private void _messagePumpTimer_Tick(object sender, EventArgs e)
        {
            try
            {
                //_cometServer.BroadcastMessage(_sendCometMsgTextBox.Text);
                string msgQueueName = ConfigurationManager.AppSettings["CometQueue"];
                MessageQueue cometMessageQueue = new MessageQueue(msgQueueName);
                System.Messaging.Message msg = new System.Messaging.Message(_message, new XmlMessageFormatter(new Type[] { typeof(string) }));
                cometMessageQueue.Send(msg);
            }
            catch (Exception ex)
            {
                string error = ex.ToString();
                //Response.Write(error);
                MessageBox.Show(error, "Comet Message Pump Error");
            }

        }

        private void _stopTestMessagesButton_Click(object sender, EventArgs e)
        {
            _messagePumpTimer.Stop();
        }
    }
}
